(function() {

    var currentCallbackId = 0

    function callNative(method, data, callback) {
        // 生成一个唯一callbackId
        var callbackId = "nativeCallback_" + (currentCallbackId++)

        if (callback) {
          // 给window添加callback
          window[callbackId] = (result) => {
              delete window[callbackId]
              callback(result)
          }
        }

        var stringData = JSON.stringify(data)
        if (window.androidBridge) {
            // android端传递三个参数
            window.androidBridge.callNative(callbackId, method, stringData)
        } else {
            // iOS不支持多参数，我们传递json对象
            window.webkit.messageHandlers.iOSBridge.postMessage({
              callbackId: callbackId,
              method: method,
              data: stringData
            })
        }
    }

    // 将方法挂到window.JSBridge下
    var JSBridge = window.JSBridge = {}
    JSBridge.callNative = callNative

    JSBridge.UI = {}
    JSBridge.UI.toast = function(message) {
      callNative('UI.toast', { message: message })
    }
})()